﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace App
{
    public partial class UserDetail : System.Web.UI.Page
    {
        AppLib.User user;
       // bool prerender = true;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Common.GetLoggedUser(this.Page) == null)
            {
                Response.Redirect("Login.aspx");
                return;
            }

            int aux = int.Parse(Server.HtmlEncode(Request.Cookies["UserDetail"].Value));
            user = MidApp.ManageUser.GetUserByID(aux);
            ImageAvatar2.DefaultImage = user.ImageURL;
            ImageAvatar2.Email = user.PrimaryEmail;
        }
        protected void Page_PreRender(object sender, EventArgs e)
        {

            SetUserInfo();

            RepeaterTeamLeader.DataSource = MidApp.ManageTeam.GetTeamsWhereUserIsLeader(user.UserID);
            RepeaterTeamLeader.DataBind();

            RepeaterTeamMember.DataSource = MidApp.ManageTeam.GetTeamsWhereUserIsMember(user.UserID);
            RepeaterTeamMember.DataBind();

        }

        protected void SetUserInfo()
        {
            ImageAvatar2.DefaultImage = user.ImageURL;
            
            LabelNick.Text = user.Nick;
            LabelName.Text = user.Name;
            LabelSurname.Text = user.Surname;
            LabelICQ.Text = user.ICQNumber;
            LabelEmail.Text = user.PrimaryEmail;
        }

        //vraci jmeno tymu, kde je user vedouci
        protected string GetTeamWhereUserIsLeader(object sender)
        {
            var aux = (AppLib.Team)sender;

            return aux.TeamName;
        }

        //vraci jmeno tymu, kde je user clenem
        protected string GetTeamWhereUserIsMember(object sender)
        {
            var aux = (AppLib.Team)sender;

            return aux.TeamName;
        }

        protected string GetTeamAvatar(object sender)
        {
            var aux = (AppLib.Team)sender;

            return aux.ImageURL;
        }
        protected string GetTeamID(object sender)
        {
            var aux = (AppLib.Team)sender;

            return aux.TeamID.ToString();
        }

        //obsluha presmerovani na detail tymu
        protected void LinkButtonTeamDetail_Click(object sender, EventArgs e)
        {
            Response.Cookies["TeamDetail"].Value = ((LinkButton)sender).CommandArgument;
            Response.Redirect("TeamDetails.aspx");
        }

        protected void ButtonStatistics_Click(object sender, EventArgs e)
        {
            Response.Redirect("UserStatistics.aspx");
        }

    }
}